package cn.hg.solon.youcan.web.admin.controller.monitor;


import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.text.CharSequenceUtil;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.core.handle.Result;
import org.noear.solon.validation.annotation.NotNull;

import com.mybatisflex.core.paginate.Page;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hg.solon.youcan.common.annotation.Log;
import cn.hg.solon.youcan.common.enums.BusinessType;
import cn.hg.solon.youcan.common.web.LayuiPage;
import cn.hg.solon.youcan.system.entity.SysUserLogin;
import cn.hg.solon.youcan.system.service.SysUserLoginService;
import cn.hg.solon.youcan.web.admin.controller.BaseAdminController;
import cn.hg.solon.youcan.web.admin.controller.monitor.qo.OperateLogQueryObject;
import cn.hg.solon.youcan.web.admin.controller.monitor.qo.UserLoginQueryObject;

/**
 * @author 胡高
 */
@Controller
@Mapping("/admin/monitor/login")
public class UserLoginController extends BaseAdminController {

    private static final String SERVICE_NAME = "登录日志";

    private static String VIEW_PATH = "/admin/monitor/login/";

    @Inject
    SysUserLoginService userLoginService;

    @Mapping(path = "clean", method = MethodType.POST)
    @Log(title = SERVICE_NAME, businessType = BusinessType.CLEAR)
    @SaCheckPermission(value = {"monitor:operate:clear"})
    public Result<?> clean(Context ctx, OperateLogQueryObject query) {
        this.log.info("清除所有[{}]记录", SERVICE_NAME);

        this.userLoginService.removeAll();

        return Result.succeed();
    }

    @Mapping(path = "", method = MethodType.GET)
    @SaCheckLogin
    public ModelAndView index(Context ctx) {
        ModelAndView mav = new ModelAndView(VIEW_PATH + "index.html");

        return mav;
    }

    /**
     * 分页查询功能
     */
    @Mapping(path = "query", method = MethodType.POST)
    @SaCheckPermission(value = {"monitor:login:query"})
    public LayuiPage<SysUserLogin> query(Context ctx, @NotNull UserLoginQueryObject query) {
        this.log.info("执行[{}]分页查询：query={}", SERVICE_NAME, query);

        query.setSortField(CharSequenceUtil.toUnderlineCase(query.getSortField()));

        /*
         * 服务调用
         */
        Page<SysUserLogin> result =
            this.userLoginService.pageByMap(new Page<>(query.getPageNumber(), query.getPageSize()),
                BeanUtil.beanToMap(query));

        /*
         * 返回值处理
         */
        return new LayuiPage<>(result.getRecords(), result.getTotalRow());
    }

    /**
     * 跳转到记录展示页面
     */
    @Mapping(path = "view", method = MethodType.GET)
    @SaCheckPermission(value = {"monitor:login:query"})
    public ModelAndView view(Context ctx, @NotNull Integer id) {
        this.log.info("跳转到[{}]展示页面：id={}", SERVICE_NAME, id);

        ModelAndView mav = new ModelAndView(VIEW_PATH + "view.html");

        mav.put("bean", this.userLoginService.getById(id));

        return mav;
    }

}
